package com.sdy.resdir.biz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.auth.api.model.UserInfo;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.resdir.biz.mapper.RdResDataItemMapper;
import com.sdy.resdir.biz.model.RdDirDataUpdateLog;
import com.sdy.resdir.biz.model.RdResDataItem;
import com.sdy.resdir.biz.model.RdResourceDirData;
import com.sdy.resdir.biz.mapper.RdResourceDirDataMapper;
import com.sdy.resdir.biz.service.RdDirDataUpdateLogService;
import com.sdy.resdir.biz.service.RdResDataItemService;
import com.sdy.resdir.biz.service.RdResourceDirDataService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源目录-数据源 服务实现类
 * </p>
 *
 * @author cgj
 * @since 2019-11-22
 */
@Slf4j
@Service
public class RdResourceDirDataServiceImpl extends BaseServiceImpl<RdResourceDirData> implements RdResourceDirDataService {
    @Resource
    private RdResourceDirDataMapper rdResourceDirDataMapper;
    @Autowired
    private RdDirDataUpdateLogService updateLogService;
    @Resource
    private RdResDataItemMapper itemMapper;
    @Autowired
    private RdResDataItemService rdResDataItemService;

    @Override
    public int updateOrSaveDirData(RdResourceDirData dirData, UserInfo user) throws BizException {
        Integer resId = dirData.getResDirId();
        if (dirData.getId() != null) {
            // 修改编目信息时不能够修改绑定资源的id
            dirData.setResDirId(dirData.getResDirId()).setModifyTime(new Date()).setModifyUserId(user.getUserId());
            int dirDataState = rdResourceDirDataMapper.updateById(dirData);
            Assert.isTrue(dirDataState == 0, "更新操作失败,请重试");
            boolean save = updateLogService.save(new RdDirDataUpdateLog().setResCatalogId(dirData.getId())
                    .setUpdateUserId(user.getUserId()).setUpdateUserName(user.getUserName()).setUpdateType(1)
                    .setUpdateTime(new Date()));
            Assert.isTrue(!save, "更新操作失败,请重试");
        } else {
            dirData.setCreateTime(new Date()).setCreatUserId(user.getUserId());
            int dirDataState = rdResourceDirDataMapper.insert(dirData);
            Assert.isTrue(dirDataState == 0, "更新操作失败,请重试");
            boolean save = updateLogService.save(new RdDirDataUpdateLog().setResCatalogId(dirData.getId())
                    .setUpdateUserId(user.getUserId()).setUpdateUserName(user.getUserName()).setUpdateType(0)
                    .setUpdateTime(new Date()));
            Assert.isTrue(!save, "更新操作失败,请重试");
        }

        List<RdResDataItem> dataItems = dirData.getDataItems();
        // 拿出新传入的值
        ArrayList<Integer> nowItemIds = new ArrayList<>();
        // 需要新增的数据
        ArrayList<RdResDataItem> rdResDataItemList = new ArrayList<>();
        for (int i = 0; i < dataItems.size(); i++) {
            if (dataItems.get(i).getId() != null) {
                nowItemIds.add(dataItems.get(i).getId());
            } else {
                rdResDataItemList.add(dataItems.get(i));
            }
        }
        // 原来存在的
        List<RdResDataItem> oldItems = rdResDataItemService.list(Wrappers.<RdResDataItem>lambdaQuery().eq(RdResDataItem::getOwnResId, resId));
        List<Integer> oldItemIds = oldItems.stream().map(RdResDataItem::getId).collect(Collectors.toList());
        for (int j = 0; j < nowItemIds.size(); j++) {
            oldItemIds.remove(nowItemIds.get(j));
        }
        // 删除从前端被删除的数据
        if (!oldItemIds.isEmpty()) {
            boolean b = rdResDataItemService.removeByIds(oldItemIds);
            Assert.notTrue(b, "删除数据时失败!");
        }
        // 找到需要新增的
        Date now = new Date();
        for (RdResDataItem item : dataItems) {
            item.setOwnResId(dirData.getResDirId()).setCreateTime(now);
        }
        if (!dataItems.isEmpty()) {
            if (!rdResDataItemList.isEmpty()) {
                int i = itemMapper.batchInsert(rdResDataItemList);
                Assert.isTrue(i == 0, "更新操作失败请重试");
            }
        }
        return 1;
    }
}
